package pl.szpadel.android.gadu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

/// Activity used as status-setting dialog
public class StatusActivity extends Activity {

	/////////////////////
	// cached controls
	
	private EditText mStatusEdit;
	private RadioGroup mStatusRadioGroup;
	
	
	////////////////////
	// implementation
	
	/// C-tor
	public StatusActivity() {
		super();
	}
	
    /// Called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // app initialisation (in case this is the first activity created)
        App.createInstance(getApplicationContext());
        
        startService(new Intent(this, CommunicationService.class));
        
        // init ui
        setContentView(R.layout.status_window);
        
        mStatusEdit = (EditText)findViewById(R.id.StatusWindowText);
        mStatusRadioGroup = (RadioGroup)findViewById(R.id.StatusRadioGroup);
        
        Button okButton = (Button)findViewById(R.id.StatusOKButton);
        okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) { onDone(); }
		});
    }
    
    /// Called when activiti is brought to top
    public void onResume() {
    	super.onResume();
    	
    	Status status = App.getInstance().getConfig().getStatus();
    	
    	mStatusEdit.setText(status.getDescription());
    	mStatusRadioGroup.clearCheck();
    	
    	switch(status.getGGStatusNoDesc()) {
    	case Status.GG_STATUS_FFC:
    		mStatusRadioGroup.check(R.id.RadioButtonFFC);
    		break;
    	case Status.GG_STATUS_AVAIL:
    		mStatusRadioGroup.check(R.id.RadioButtonAvailable);
    		break;
    	case Status.GG_STATUS_BUSY:
    		mStatusRadioGroup.check(R.id.RadioButtonAway);
    		break;
    	case Status.GG_STATUS_DND:
    		mStatusRadioGroup.check(R.id.RadioButtonDND);
    		break;
    	case Status.GG_STATUS_INVISIBLE:
    		mStatusRadioGroup.check(R.id.RadioButtonInvisible);
    		break;
    	default:
    		mStatusRadioGroup.check(R.id.RadioButtonAvailable);
    	}
    }
    
    
    /// Handles input event ending the dialog
    private void onDone() {
    	
    	int selectedRadioButton = mStatusRadioGroup.getCheckedRadioButtonId();
    	int ggStatus = Status.GG_STATUS_AVAIL_DESCR;
    	
    	switch(selectedRadioButton) {
    	case R.id.RadioButtonFFC:
    		ggStatus = Status.GG_STATUS_FFC_DESCR;
    		break;
    	case R.id.RadioButtonAway:
    		ggStatus = Status.GG_STATUS_BUSY_DESCR;
    		break;
    	case R.id.RadioButtonInvisible:
    		ggStatus = Status.GG_STATUS_INVISIBLE_DESCR;
    		break;
    	case R.id.RadioButtonDND:
    		ggStatus = Status.GG_STATUS_DND_DESCR;
    		break;
    	default:
    		ggStatus = Status.GG_STATUS_AVAIL_DESCR;
    	}
    	
    	String desc = mStatusEdit.getText().toString();
    	Status status = new Status(ggStatus, desc);
    	
    	App.getInstance().getCommunicationService().setStatus(status);
    	
    	finish();
    	
    }

}
